﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AssessmentActions;

public partial class Teacher_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnFetchHTML_Click(object sender, EventArgs e)
    {
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(strConnString);
        short parseResult = 0;
        if (Int16.TryParse(ddListTestNumbers.SelectedValue, out parseResult))
        {
            int testId = Int16.Parse(ddListTestNumbers.SelectedValue);
            try
            {
                string testString = ada.fetchTestHtml(testId, false);
                fetchedTestLabel.Text = "<div style=\"background-color: #fff; border: 2px solid #777;\">" + testString + "</div>";
            }
            catch
            {
                fetchedTestLabel.Text = "Invalid test number.";
            }
        }
        else
        {
            fetchedTestLabel.Text = "Invalid test number.";
        }

        fetchedTestLabel.Text = "";
    }

    protected void btnFetchXML_Click(object sender, EventArgs e)
    {
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(strConnString);
        short parseResult = 0;
        if (Int16.TryParse(ddListTestNumbers.SelectedValue, out parseResult))
        {
            int testId = Int16.Parse(ddListTestNumbers.SelectedValue);
            try
            {
                string testString = ada.fetchTestXml(testId, false);
                testString = Server.HtmlEncode(testString);
                fetchedTestLabel.Text = "<pre style=\"background-color: #fff; border: 2px solid #777;\">" + testString + "</pre>";
            }
            catch
            {
                fetchedTestLabel.Text = "Invalid test number.";
            }
        }
        else
        {
            fetchedTestLabel.Text = "Invalid test number.";
        }
        fetchedTestLabel.Text = "";
    }
    protected void btnFetchHTMLwithAnswers_Click(object sender, EventArgs e)
    {
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(strConnString);
        short parseResult = 0;
        if (Int16.TryParse(ddListTestNumbers.SelectedValue, out parseResult))
        {
            int testId = Int16.Parse(ddListTestNumbers.SelectedValue);
            try
            {
                string testString = ada.fetchTestHtml(testId, true);
                fetchedTestLabel.Text = "<div style=\"background-color: #fff; border: 2px solid #777;\">" + testString + "</div>";
            }
            catch
            {
                fetchedTestLabel.Text = "Invalid test number.";
            }
        }
        else
        {
            fetchedTestLabel.Text = "Invalid test number.";
        }
        fetchedTestLabel.Text = "";
    }
    protected void btnFetchXMLwithAnswers_Click(object sender, EventArgs e)
    {
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(strConnString);
        short parseResult = 0;
        if (Int16.TryParse(ddListTestNumbers.SelectedValue, out parseResult))
        {
            int testId = Int16.Parse(ddListTestNumbers.SelectedValue);
            try
            {
                string testString = ada.fetchTestXml(testId, true);
                testString = Server.HtmlEncode(testString);
                fetchedTestLabel.Text = "<pre style=\"background-color: #fff; border: 2px solid #777;\">" + testString + "</pre>";
            }
            catch
            {
                fetchedTestLabel.Text = "Invalid test number.";
            }
        }
        else
        {
            fetchedTestLabel.Text = "Invalid test number.";
        }
        fetchedTestLabel.Text = "";
    }

    protected void btnFetchFormattedForTesting_Click(object sender, EventArgs e)
    {
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

        AssessmentDatabaseActions ada = new AssessmentDatabaseActions(strConnString);
        short parseResult = 0;
        if (Int16.TryParse(ddListTestNumbers.SelectedValue, out parseResult))
        {
            int testId = Int16.Parse(ddListTestNumbers.SelectedValue);
            try
            {
                Assessment assessment = ada.getAssessmentFromId(testId);
                string serialized_assessment = TestingLibrary.TestingFunctions.serializeAssessment(assessment);
                TestViewControl.setAssessment(serialized_assessment);
            }
            catch
            {
                fetchedTestLabel.Text = "Invalid test number.";
            }
        }
        else
        {
            fetchedTestLabel.Text = "Invalid test number.";
        }
        fetchedTestLabel.Text = "";
    }
}